package com.cxmx.escort.common.utils.result;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;

public class JsonUtil {

    /**
     * 获得json格式字符串
     *
     * @param
     * @param
     * @param
     * @return
     */
    public static String parseMapJson(Map<String, Object> map) {
        return JSONObject.toJSONString(map, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNonStringValueAsString, SerializerFeature.WriteNullListAsEmpty);
    }

    /**
     * 获得json格式字符串
     *
     * @param
     * @param
     * @param
     * @return
     */
    public static String parseListJson(List<Map<String, Object>> list) {
        return JSONObject.toJSONString(list, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNonStringValueAsString, SerializerFeature.WriteNullListAsEmpty);

    }

    /**
     * 获得json格式字符串
     *
     * @param
     * @param
     * @param
     * @return
     */
    public static String parseListJsonNew(List<? extends Object> list) {
        return JSONObject.toJSONString(list, SerializerFeature.WriteNonStringValueAsString, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullListAsEmpty);
    }

    public static String parseListfobjJson(List<Object> list) {
        return JSONObject.toJSONString(list, SerializerFeature.WriteNonStringValueAsString, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullListAsEmpty);
    }

    /**
     * 获得json格式字符串
     *
     * @param obj
     * @param msg
     * @param status
     * @return
     */
    public static String parseJson(Object obj, String msg, int status) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("msg", msg);
        map.put("code", status);
        map.put("data", obj == null ? new Object() : obj);
        return JSONObject.toJSONString(map, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNonStringValueAsString, SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullListAsEmpty);
    }


    public static String parseJson(Object obj, Object msg, int status) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("msg", msg == null ? new Object() : msg);
        map.put("code", status);
        map.put("data", obj == null ? new Object() : obj);
        return JSONObject.toJSONString(map, SerializerFeature.WriteNonStringValueAsString, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullListAsEmpty);
    }


    /**
     * 获得json格式字符串
     *
     * @param
     * @param msg
     * @param status
     * @return
     */
    public static String parseJson(String msg, int status) {
        return parseJson(null, msg, status);
    }

    /**
     * 获得json格式字符串
     *
     * @param obj
     * @param msg
     * @param
     * @return
     */
    public static String parseSuccessJson(Object obj, String msg) {
        return parseJson(obj, msg, ResultCode.SUCCESS.getCode());
    }

    /**
     * 获得json格式字符串
     *
     * @param
     * @param msg
     * @param
     * @return
     */
    public static String parseSuccessJson(String msg) {
        return parseJson(null, msg, ResultCode.SUCCESS.getCode());
    }

    /**
     * 获得json格式字符串
     *
     * @param obj
     * @param msg
     * @param
     * @return
     */
    public static String parseFailJson(Object obj, String msg) {
        return parseJson(obj, msg, ResultCode.FAIL.getCode());
    }

    public static String parseFailJson(Object obj, String msg, int status) {
        return parseJson(obj, msg, status);
    }

    public static String parseFailJson(Object obj, Object msg) {
        return parseJson(obj, msg, ResultCode.FAIL.getCode());
    }


    /**
     * 获得json格式字符串
     *
     * @param
     * @param msg
     * @param
     * @return
     */
    public static String parseFailJson(String msg) {
        return parseJson(null, msg, ResultCode.FAIL.getCode());
    }

    /**
     * 转化成json格式
     *
     * @param str
     * @return
     */
    public static String getJsonFormat(Object str) {
        try {
            return JSON.toJSONString(str);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 转换jsonObj
     *
     * @param str
     * @return
     */
    public static JSONObject getJsonObj(String str) {
        return JSON.parseObject(str);
    }

    public static SortedMap<String, String> convertJsonStrToMap(String jsonStr) {

        SortedMap<String, String> map = JSON.parseObject(
                jsonStr, new TypeReference<SortedMap<String, String>>() {
                });

        return map;
    }


    /**
     * APP端获得json格式字符串
     *
     * @param obj
     * @param msg
     * @param status
     * @return
     */
    public static String parseJsonApp(Object obj, String msg, int status) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("msg", msg);
        map.put("response", status);
        map.put("data", obj == null ? new Object() : obj);
        return JSONObject.toJSONString(map, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNonStringValueAsString, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullListAsEmpty);
    }

    public static String parseFailAppJson(Object obj, String msg) {
        return parseJsonApp(obj, msg, ResultCode.FAIL.getCode());
    }


    /**
     * 获得json格式字符串
     *
     * @param obj
     * @param msg FAIL_STATUS
     * @param
     * @return
     */
    public static String parseSuccessAppJson(Object obj, String msg) {
        return parseJsonApp(obj, msg, ResultCode.FAIL.getCode());
    }


    public static void main(String[] args) {
        String str = "http%3A%2F%2Fsz.cxmx90.com%2F%3Fcode%3D071kewKv0WKbzj105BJv0QkoKv0kewKN%26state%3D123%23%2Fblockchain%3FcourseId%3D1";
        System.out.println(URLDecoder.decode(str));
    }
}
